package com.qian.api.ddj.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;


@Component
public class EmailUtil {

    @Value("${spring.mail.username}")
    private String sendFrom;

    @Autowired
    private JavaMailSender mailSender;


    public void bingEmail(String sendTo, String content, String title){
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(sendFrom);//发送者.
//        message.setTo(sendTo);//接收者.
//        message.setSubject(title);//邮件主题.
//        message.setText(content);//邮件内容.

        MimeMessage message = null;
        try {
            message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendFrom);
            helper.setTo(sendTo);
            helper.setSubject(title);
            helper.setText(content, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mailSender.send(message);
    }


}
